using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Controllers
{
	#region using
	using System.Web.Mvc;
	using WebClient.Models.ViewModels;
	using WebClient.Models;
	using WebClient.Models.Repositories;
	using WebClient.Helpers;
	using System.Web.Security;
    using WebClient.CF_DAL_WS;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectCity()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CITY
			};
			return View(CreateViewModel(data));
		}

		public ActionResult DeleteCity(int id, int page = 1)
		{
			var result = _repCity.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_CITY,
				CurrentPage = page
			};
			return View("SelectCity", CreateViewModel(data));
		}

		public ActionResult EditCity(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CITY,
				CurrentPage = page,
				IntID = id
			};
			return PartialView("Templates/TH_EditCity", CreateViewModel(data));
		}

		public ActionResult CancelEditingCity(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CITY,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListCity", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateCity(FormCollection forms)
		{
			string id = forms.GetValues("City_ID")[0];
			string name = forms.GetValues("City_Name")[0];
			var city = _repCity.SelectByID(int.Parse(id));
			city.Name = name;
			var result = _repCity.Update(city);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CITY,
			};
			return View("SelectCity", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertCity(FormCollection forms)
		{
			string name = forms.GetValues("City_Name")[0];
			var newItem = new CityDTO
			{
				Name = name
			};
			var result = _repCity.Insert(newItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CITY,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectCity", CreateViewModel(data));
		}


	}
}